import {createSlice, PayloadAction} from "@reduxjs/toolkit";
import {RootState} from "../store";

export interface EduInfo {
    beginDate: string,
    endDate: string,
    schoolName: string,
    major: string,
    degree: string,
    detail: string
}
export type EduInfoList = EduInfo[]

/*
============ 教育经历 =============
开始时间
结束时间
学校名称
专业
学历
详细描述
*/
export const eduInitialState: EduInfoList = [
    {
        beginDate: '',
        endDate: '',
        schoolName: '',
        major: '',
        degree: '',
        detail: ''
    }
]

const eduInfoSlice = createSlice({
    initialState: eduInitialState,
    name: 'eduInfo',
    reducers: {
        reInitEduInfo: (state, action) => {
           return action.payload;
        },
        addEduInfoItem: (state) => {
            state.push({
                beginDate: '', endDate: '',
                schoolName: '', major: '',
                degree: '', detail: ''
            })
        },
        updateEduInfo: (state, action: PayloadAction<{index: number, updateValue: {[p in keyof EduInfo]?: string}} >) => {
            const { index, updateValue } = action.payload
            state[index] = {...state[index], ...updateValue}
        },
        deleteEduInfo: (state, action: PayloadAction<{index: number}>) => {
            const { index } = action.payload
            state.splice(index, 1)
        }
    }
})
export const {
    addEduInfoItem,
    updateEduInfo,
    deleteEduInfo,
    reInitEduInfo
} = eduInfoSlice.actions;

export const getEduInfo = (state: RootState) => state.eduExperience

export default eduInfoSlice.reducer;
